﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileChangeNotification.cs" company="" />
// <summary>
//   A notification for a file change.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Utilities.FileChangesTracking
{
    using System;

    /// <summary>
    /// A notification for a file change.
    /// </summary>
    public class FileChangeNotification : IEquatable<FileChangeNotification>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileChangeNotification"/> class.
        /// </summary>
        /// <param name="filePath">The path of the file undergoing the change.</param>
        /// <param name="changeEssence">The essence of the change.</param>
        /// <param name="newPath">Denotes the new path for the file, in case this describes a rename-operation change.</param>
        public FileChangeNotification(string filePath, FileChangeType changeEssence, string newPath = null)
        {
            FilePath = filePath;
            ChangeEssence = changeEssence;
            NewPath = newPath;
        }

        /// <summary>
        /// Gets the path of the file which change is described here.
        /// </summary>
        public string FilePath { get; private set; }

        /// <summary>
        /// Gets the essence of the change in the file.
        /// </summary>
        public FileChangeType ChangeEssence { get; private set; }

        /// <summary>
        /// Gets the new name of the file, in case it is a rename-operation change.
        /// </summary>
        public string NewPath { get; private set; }

        #region IEquatable<> implementation

        public bool Equals(FileChangeNotification other)
        {
            if (other == null)
            {
                return false;
            }

            return FilePath.Equals(other.FilePath) && ChangeEssence.Equals(other.ChangeEssence);
        }

        #endregion

        #region object overrides

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            var otherChangeNotification = obj as FileChangeNotification;
            if (otherChangeNotification != null)
            {
                return Equals(otherChangeNotification);
            }

            return Equals(this, obj);
        }

        public override int GetHashCode()
        {
            return FilePath.GetHashCode() ^ ChangeEssence.GetHashCode();
        }

        #endregion
    }
}
